// pages/user_end/posters/posters.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '',

  },
  saveImage() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定保存到相册吗',
      complete: (res) => {
        if (res.confirm) {

          wx.downloadFile({
            url: that.data.path, //图片地址
            success: (res) => {
              if (res.statusCode === 200) {
                wx.saveImageToPhotosAlbum({
                  filePath: res.tempFilePath,
                  success: function () {
                    wx.showToast({
                      title: "保存成功",
                      icon: "none"
                    });
                  },
                  fail: function () {
                    wx.showToast({
                      title: "保存失败",
                      icon: "none"
                    });
                  }
                });
              }
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
          that.getInfo();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: '邀请好友注册',
      imageUrl:that.data.path,
      path: '/pages/user_end/index/index'
     }
  },
  getInfo() {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getposter, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      that.setData({
        path: res.data.data
      })

    })
  },
  confirmsave(){
    var that = this;
    wx.showModal({
      title:'提示',
      content:'是否保存图片分享到朋友圈',
      success:function(res){
         if(res.confirm){
             that.saveImage();
         }
      }
    })
  }
})